#include "SnapshotProject.h"
#include <cassert>
#include <qstackedwidget.h>

SnapshotPage::SnapshotPage( SnapshotItemPtr proj, QStackedWidget * parent )	
	: QWidget(parent), mParent( parent ), mProject( proj )
{
	assert( parent );

	ui.setupUi(this);
	parent->addWidget(this);
}
SnapshotPage::~SnapshotPage(){
}
void SnapshotPage::clearContents()
{
	getTable()->clearContents();
	getTable()->setRowCount( 0 );
}

int SnapshotPage::addRow( const StringVector& labels )
{
	int id = ui.projectTable->rowCount();
	ui.projectTable->insertRow( id );
	for( size_t i = 0; i<labels.size(); i++ )
		ui.projectTable->setItem( id, i, new QTableWidgetItem( labels[i] ) );
	return id;
}
int SnapshotPage::index() const { return mParent?mParent->indexOf( (QWidget*)this ):-1; }
bool SnapshotPage::isOpen() const{ return index() != -1; }
void SnapshotPage::open(){ 
	if( !mParent  ) return;
	if( !isOpen() ) mParent->addWidget( this );
	setCurrent( );
}
void SnapshotPage::close(){
	if( !mParent  ) return; 
	if( !isOpen() ) return;
	mParent->removeWidget( this );
}
void SnapshotPage::setCurrent( )
{
	if( !isOpen() ) open();
	if( mParent ) mParent->setCurrentWidget( this );
}

bool SnapshotPage::isCurrent() const{ return (mParent?(mParent->currentWidget()==this):false); }
	
QTableWidget * SnapshotPage::getTable(){ return ui.projectTable; }
const QTableWidget * SnapshotPage::getTable() const{ return ui.projectTable; }

SnapshotItem::SnapshotItem ( bool isRepo, const String &name, const QEnvPath &path, const String & branch ): mAdded( false ), mItem(NULL), mRepository( isRepo ), mPage(NULL), mName(name), mPath(path), mBranch(branch){}
SnapshotItem::SnapshotItem ( const SnapshotItem &rk ): mItem( rk.mItem ), mRepository(rk.mRepository), mName(rk.mName), mAdded( rk.mAdded ), mPath(rk.mPath), mPage(rk.mPage), mBranch( rk.mBranch){}
SnapshotItem::~SnapshotItem (){ 
	if( mPage ) delete mPage, mPage = NULL; 
}
SnapshotItem& SnapshotItem::operator= ( const SnapshotItem &rk ){ mRepository=rk.mRepository; mItem = rk.mItem; mName=rk.mName; mPath=rk.mPath; mPage = rk.mPage; mBranch=rk.mBranch; return *this; }

void SnapshotItem::setPage( SnapshotPage* item ){ mPage = item; }
SnapshotPage* SnapshotItem::getPage() const{ return mPage; }

void SnapshotItem::setItem( QTreeWidgetItem * item ){ mItem = item; }
QTreeWidgetItem * SnapshotItem::getItem() const{ return mItem; }

const String & SnapshotItem::getName() const{ return mName; }
const QEnvPath& SnapshotItem::getPath() const{ return mPath; }
const String& SnapshotItem::getBranch() const{ return mBranch; }

void SnapshotItem::setBranch( const String & branch){ mBranch=branch; }
void SnapshotItem::setName( const String & name){ mName=name; }
void SnapshotItem::setPath(const QEnvPath& path){ mPath=path; }



SnapshotProject::SnapshotProject (QWidget * parent) : QTreeWidget( parent ), mStack(NULL)
{
	connect( (QTreeWidget*)this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT( _projectStateChanged(QTreeWidgetItem*, int)) );
	connect( (QTreeWidget*)this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT( _showProjectPage(QTreeWidgetItem*, int)) );
}
SnapshotProject::SnapshotProject ( const SnapshotProject &rk ) : Type( rk ){}
SnapshotProject::~SnapshotProject (){ clear(); }

void SnapshotProject::setStackWidget( QStackedWidget * tabs ){ 
	mStack = tabs; 
}
QStackedWidget* SnapshotProject::getStackWidget(){ return mStack; }
const QStackedWidget* SnapshotProject::getStackWidget() const{ return mStack; }
bool SnapshotProject::hasItem( const String &name ) const{ return Type::find(name.data()) != end(); }

void SnapshotProject::clear()
{
	QTreeWidget::clear();
	for( auto i=begin(); i!=end(); i++ )
	{
		if( i->second )
		{
			mStack->removeWidget( i->second->getPage() );
			i->second.destroy(true);
			i->second.setDestroy(false);
		}
	}
	Type::clear();
}
SnapshotProject& SnapshotProject::push_back( Item * itm )
{
	if( !itm ) return *this;
	if( hasItem( itm->getName() ) ) return *this;
	Type::iterator it=Type::find(itm->getPath());
	if( it == end() )
		it = insert( begin(), std::make_pair( itm->getName().data(), SnapshotItemPtr(itm) ) );
	it->second.setDestroy(true);
	return *this;
}
bool SnapshotProject::remove( const String &name )
{
	Type::iterator it = Type::find(name);
	if( it == end() ) return false;
	erase( it );
	return true;
}
bool SnapshotProject::remove( Item * itm ){ return itm?remove(itm->getName()):false; }
void SnapshotProject::_closeProjectPage(int id)
{
	for( auto i=begin(); i!=end(); i++ )
	{
		SnapshotPage * page = i->second->getPage();
		if( page && page->index() == id )
		{
			page->close();
		}
	}
}
void SnapshotProject::updateAdded( SnapshotItemPtr ptr )
{
		
}
void SnapshotProject::refreshSnapshot( ){
	if( !mList ) return;
	mList->clear();
	mList->setRowCount( 0 );
	mList->setColumnCount( 1 );
	mList->horizontalHeader()->setVisible( false );
	mList->horizontalHeader()->setStretchLastSection( true );
	mList->verticalHeader()->setVisible(false);
	for( auto i=begin(); i!=end(); i++ )
	{
		if( !i->second->isRepository() && i->second->isAdded() ) 
		{
			int id = mList->rowCount();
			mList->insertRow( id );
			mList->setItem( id, 0, new QTableWidgetItem( i->second->getPath().path() ) );
		}
	}
}
void SnapshotProject::setSnapshotTable( QTableWidget * lst ){ mList = lst; }
void SnapshotProject::_projectStateChanged(QTreeWidgetItem* ptr, int c)
{
	if( !ptr ) return;
	
	for( auto i=begin(); i!=end(); i++ )
	{
		if( i->second->getItem() == ptr )
		{
			i->second->setAdded( ptr->checkState(c) == Qt::Checked );
			break;
		}
	}
	refreshSnapshot();
}
void SnapshotProject::_showProjectPage( QTreeWidgetItem * ptr, int )
{
	if( !ptr ) return;
	
	for( auto i=begin(); i!=end(); i++ )
	{
		SnapshotPage * page = i->second->getPage();
		SnapshotItem * item = *i->second;
		
		if( i->second->getItem() == ptr )
		{
			if( item->isRepository() )
			{
				Qt::CheckState state = item->getItem()->checkState( 0 );
				for( int id=0; id<item->getItem()->childCount(); id++ )
				{
					QTreeWidgetItem * child=item->getItem()->child(id);
					if( child ) child->setCheckState( 0, state );
				}
			}
			else
			{
				if( page )
				{
					page->open();
					page->clearContents();
					page->addRow( StringVector() << item->getName() << item->getBranch() << item->getPath().path() << "0 files" );
				}
			}
		}
	}
}